//
//  DetailViewController.m
//  HomeIn
//
//  Created by dllo on 15/11/7.
//  Copyright © 2015年 ZZP. All rights reserved.
//

#import "DetailViewController.h"
#import "FirstPageModel.h"
#import "CommentViewController.h"

@interface DetailViewController ()<UIWebViewDelegate,UMSocialUIDelegate>

@property(nonatomic,strong)UIWebView *webView;
@property (nonatomic, retain) UIView *bottomView;//底部视图

@end

@implementation DetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //导航栏标题
    self.navigationItem.title = self.firstPageModel.title;
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20],NSForegroundColorAttributeName:[UIColor whiteColor]}];
    
    //导航栏左侧按钮
    UIImage *image = [UIImage imageNamed:@"iconfont-unie642.png"];
    //取消渲染
    image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(leftBarAction)];
    
    //网页
    self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - HEIGHT / 16 )];
    //请求数据
    NSString *string = self.firstPageModel.content_url;
    NSURL *url = [NSURL URLWithString:string];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [self.webView loadRequest:request];
    self.webView.delegate = self;
    [self.view addSubview:self.webView];
    
    //解决webview到边框下拉有灰块
    for (UIScrollView* view in  self.webView.subviews)
    {
        if ([view isKindOfClass:[UIScrollView class]])
        {
            view.bounces = NO;
        }
    }
    //self.webView.backgroundColor = [UIColor clearColor];
    //[self.webView setOpaque:NO];//设置控件是否透明
    

    //底部 3个button
    self.bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, HEIGHT - HEIGHT / 16 - 64, WIDTH, HEIGHT / 16)];
    self.bottomView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.bottomView];

    for (int i = 0; i < 3; i++)
    {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(10 + WIDTH / 3 * i + 20, 5, WIDTH / 12, HEIGHT / 22);
        button.tag = 2000 + i;
        [button setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"button%d.png", i]] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.bottomView addSubview:button];
    }
   
}

////webview滑到底部
//- (void)webViewDidFinishLoad:(UIWebView *)webView{
//    
//    NSInteger height = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight;"] intValue];
//    NSString* javascript = [NSString stringWithFormat:@"window.scrollBy(0, %ld);", height];
//    [webView stringByEvaluatingJavaScriptFromString:javascript];
//}

- (void)buttonAction:(UIButton *)btn
{
    //收藏按钮
    if (btn.tag == 2000)
    {
        
        NSString *str=[[NSString alloc]init];
        NSMutableArray *array = [[DataHandle shareDataBaseHandle] selectJingxuan];
        for (FirstPageModel *model in array) {
            if ([model.content_url isEqual:self.firstPageModel.content_url]) {
                str = model.content_url;
            }
        }
        
        if(![str isEqualToString:@""])
        {
            [[DataHandle shareDataBaseHandle] deleteJingxuanTable:self.firstPageModel.content_url];
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示:取消收藏" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
            
        }
        else
        {
            [[DataHandle shareDataBaseHandle] insertJingxuanCellModelTable:self.firstPageModel];
            
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"收藏成功" message:@"恭喜你收藏成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
            
        }
    
    }
    if (btn.tag == 2001)
    {
        //图片
        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:self.firstPageModel.cover_image_url]];

        [UMSocialSnsService presentSnsIconSheetView:self
                                             appKey:@"507fcab25270157b37000010"
                                          shareText:self.firstPageModel.title
                                         shareImage:[UIImage imageWithData:data]
                                    shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToTencent,nil]
                                           delegate:self];
        
    }
    if (btn.tag == 2002)
    {
        //进入评论页面
        CommentViewController *commentVC = [[CommentViewController alloc] init];
        commentVC.firstPageModel = self.firstPageModel;
        
        [self.navigationController pushViewController:commentVC animated:YES];

    }
}

//图片下拉放大
//-(void)scrollViewDidScroll:(UIScrollView *)scrollView
//{
//    if (scrollView==self.webView.scrollView) {
//        CGFloat y = scrollView.contentOffset.y + 64;//加上导航栏高度,第一次是-200
//        if (y < -200*HEIGHT) {
//            CGRect frame = self.myImageView.frame;
//            frame.origin.y = y;//imageView的frame是不断往上偏移
//            frame.size.height =  -y;//tablview向下偏移了多少,高度就增加多少
//            self.myImageView.frame = frame;
//        }
//    }
//}

- (void)leftBarAction
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
